package cz.proks.schoolpupilbook.gwt.widget.common;

import java.util.LinkedList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import cz.proks.schoolpupilbook.gwt.core.SchoolPupilBookFactory;

public class CourseForm extends Composite {
	interface CourseFormUiBinder extends UiBinder<Widget, CourseForm> {
	}
	
	private static CourseFormUiBinder uiBinder = GWT.create(CourseFormUiBinder.class);
	
	@UiField Errors errors;
	@UiField Grid table;
	@UiField TextBox idBox;
	@UiField TextBox codeBox;
	@UiField TextBox nameBox;

	public CourseForm() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	public String getId() {
		return idBox.getValue();
	}
	
	public void setId(String id) {
		this.idBox.setValue(id);
	}
	
	public String getCode() {
		return codeBox.getValue();
	}
	
	public void setCode(String code) {
		this.codeBox.setValue(code);
	}
	
	public String getName() {
		return nameBox.getValue();
	}
	
	public void setName(String name) {
		this.nameBox.setValue(name);
	}
	
	public void setIdReadOnly(boolean readOnly) {
		idBox.setReadOnly(readOnly);
	}

	public void setCodeReadOnly(boolean readOnly) {
		codeBox.setReadOnly(readOnly);
	}
	
	public void setNameReadOnly(boolean readOnly) {
		nameBox.setReadOnly(readOnly);
	}
	
	@UiHandler("submitButton")
	public void submit(ClickEvent event) {
		LinkedList<String> errors = validate();
		if (errors.isEmpty()) {
			this.errors.setErrors(null);
			SubmitEvent submitEevent = new SubmitEvent(getId(), getCode(), getName());
			SchoolPupilBookFactory.getEventBus().fireEvent(submitEevent);			
		} else {
			this.errors.setErrors(errors);			
		}
	}
	
	@UiHandler("backButton")
	public void back(ClickEvent event) {
		History.back();
	}
	
	private LinkedList<String> validate() {
		LinkedList<String> errors = new LinkedList<String>();
		
		if (getCode() == null || getCode().isEmpty())
			errors.add("Code field is empty");
		
		if (getName()== null || getName().isEmpty())
			errors.add("Name field is empty");
		
		return errors;
	}
	
	public static class SubmitEvent extends GwtEvent<SubmitEvent.SubmitEventHandler> {
		public interface SubmitEventHandler extends EventHandler {
			void onSubmit(SubmitEvent event);
		}
		
		public final static Type<SubmitEvent.SubmitEventHandler> TYPE = new Type<SubmitEvent.SubmitEventHandler>();
		
		private String id;
		private String name;
		private String code;
		
		public SubmitEvent(String id, String code, String name) {
			super();
			this.id = id;
			this.name = name;
			this.code = code;
		}
		
		@Override
		public Type<SubmitEventHandler> getAssociatedType() {
			return TYPE;
		}
		
		@Override
		protected void dispatch(SubmitEventHandler handler) {
			handler.onSubmit(this);			
		}
		
		public String getId() {
			return id;
		}

		public String getName() {
			return name;
		}

		public String getCode() {
			return code;
		}				
	}

}
